package com.xwli.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xwli.springcloud.projo.Dept;
import com.xwli.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xwli
 * @date: 2021/9/6 20:55
 * @desc  提供RestFul服务
 * @since JDK 1.8
 */

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/dept/get/{deptNo}")
    @HystrixCommand(fallbackMethod = "hystrixGetDeptByDeptNo")
    public Dept getDeptByDeptNo(@PathVariable("deptNo") Long deptNo){
        Dept dept = deptService.getDeptByDeptNo(deptNo);
        if(dept == null){
            throw new RuntimeException("id===>"+deptNo+",不存在该用户，或者无法查询到信息！");
        }
        return dept;
    }

    //备选方法
    public Dept hystrixGetDeptByDeptNo(@PathVariable("deptNo") Long deptNo){
        return new Dept()
                .setDeptNo(deptNo)
                .setDeptName("id===>"+deptNo+"没有对应的信息，null===@Hystrix")
                .setDb_source("no this database in MySQL");
    }

}

